<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: function_base.php 31649 2016-01-21 04:24:56Z mpage $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

function cp_perm($plugin, $ac) {
	global $_G;
	
	$query = C::t('common_plugin')->fetch_all_data();
	foreach($query as $value) {
		if(strpos($value['identifier'], 'dzapp_') !== 0) continue;
		@include_once DISCUZ_ROOT.'./source/plugin/'.$value['directory'].'include/shopcp_nav.php';
	}
	usort($_G['shopcp_nav'], 'basecmp');

	if($_G['adminid'] == 1) return;
}

function thumb_get($pic, $width, $height) {
	
	return 'plugin.php?id=dzapp_base&mod=image&pic='.$pic.'&size='.$width.'_'.$height;
}

function array_col($array, $key) {
	global $_G;

	foreach($array as $value) {
		$return[] = $value[$key];
	}
	$return = array_unique($return);

	return $return;
}

function dzapp_template($file, $path = '') {
	global $_G;

	loadcache('dzapp_template');
	list($identifier, $tpl) = explode(':', $file);
	$diy = $_G['mobile'] ? '' : 'diy:';
	if(empty($path)) {
		$type = $_G['mobile'] ? 'wap' : 'pc';
		$path = $_G['cache']['dzapp_template'][$identifier][$type] ? $_G['cache']['dzapp_template'][$identifier][$type] : 'source/plugin/'.$identifier.'/template';
	}

	return template($diy.$tpl, 0, $path);
}

function cate_children($cate_id, $cache) {
	global $_G;

	$cate = $_G['cache'][$cache][$cate_id];
	if($cate['upid'] == 0 && $cate['children']) {
		$cate_id = array_merge($cate['children'], array($cate_id));
	}

	return $cate_id;
}

function dzapp_pic_upload($FILES, $type='album', $thumb_width=0, $thumb_height=0, $thumb_type=2) {
	$upload = new discuz_upload();

	$result = array('pic'=>'', 'thumb'=>0, 'remote'=>0);

	$upload->init($FILES, $type);
	if($upload->error()) {
		return array();
	}

	$upload->save();
	if($upload->error()) {
		return array();
	}

	$result['pic'] = $upload->attach['attachment'];

	if($thumb_width && $thumb_height) {
		require_once libfile('class/image');
		$image = new image();
		if($image->Thumb($upload->attach['target'], '', $thumb_width, $thumb_height, $thumb_type)) {
			$result['thumb'] = 1;
		}
	}

	if(getglobal('setting/ftp/on')) {
		if(ftpcmd('upload', $type.'/'.$upload->attach['attachment'])) {
			if($result['thumb']) {
				ftpcmd('upload', $type.'/'.getimgthumbname($upload->attach['attachment']));
			}
			ftpcmd('close');
			$result['remote'] = 1;
		} else {
			if(getglobal('setting/ftp/mirror')) {
				@unlink($upload->attach['target']);
				@unlink(getimgthumbname($upload->attach['target']));
				return array();
			}
		}
	}

	return $result;
}

function dzapp_pic_delete($pic, $type, $thumb, $remote) {
	global $_G;

	if($remote > 1 && $type == 'album') {
		$remote -= 2;
		$type = 'forum';
		return true;
	}

	if($remote) {
		ftpcmd('delete', $type.'/'.$pic);
		if($thumb) {
			ftpcmd('delete', $type.'/'.getimgthumbname($pic));
		}
		ftpcmd('close');
	} else {
		@unlink($_G['setting']['attachdir'].'/'.$type.'/'.$pic);
		if($thumb) {
			@unlink($_G['setting']['attachdir'].'/'.$type.'/'.getimgthumbname($pic));
		}
	}
	return true;
}

function dzapp_pic_get($filepath, $type, $thumb, $remote, $return_thumb=1, $hastype = '') {
	global $_G;

	$url = $filepath;
	if($return_thumb && $thumb) $url = getimgthumbname($url);
	if($remote > 1 && $type == 'album') {
		$remote -= 2;
		$type = 'forum';
	}
	$type = $hastype ? '' : $type.'/';
	return ($remote?$_G['setting']['ftp']['attachurl']:$_G['setting']['attachurl']).$type.$url;
}

function page_url($gets) {
	$arr = array();
	foreach ($gets as $key => $value) {
		if($value) {
			$arr[] = $key.'='.urlencode($value);
		}
	}
	return implode('&', $arr);
}

function CateForLevel($cate, $upid = 0, $level = 0, $pk = 'cate_id') {
	$arr = array();
	foreach($cate as $v) {
		if($v['upid'] == $upid) {
			$v['level'] = $level + 1;
			$arr[$v[$pk]] = $v;
			$arr = array_merge($arr, CateForLevel($cate, $v[$pk], $level+1, $pk));
		}
	}
	return $arr;
}

function CateForChild($cate, $upid = 0, $pk = 'cate_id') {
	$arr = array();
	foreach($cate as $v) {
		if($v['upid'] == $upid) {
			$v['child'] = CateForChild($cate, $v[$pk], $pk);
			$arr[$v[$pk]] = $v;
		}
	}
	return $arr;
}

function getChildsId($cate, $upid, $pk = 'cate_id') {
	$arr = array();
	foreach($cate as $v) {
		if($v['upid'] == $upid) {
			$arr[$v[$pk]] = $v[$pk];
			$arr = array_merge($arr, getChildsId($cate, $v[$pk], $pk));
		}
	}
	return $arr;
}

function getFatherId($cate, $upid, $pk = 'cate_id') {
	$arr = array();
	foreach($cate as $v) {
		if($v[$pk] == $upid) {
			$arr[$v[$pk]] = $v;
			$arr = array_merge(getFatherId($cate, $v['upid'], $pk), $arr);
		}
	}
	return $arr;
}

function wxpay_init($redirect_uri) {
	global $_G;

	$_G['wxpay'] = in_array('dzapp_wxpay', $_G['setting']['plugins']['available']) && strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger');
	if($_G['mobile'] && $_G['wxpay']) {
		if(empty($_G['cookie']['openid'])) {
			$appid = $_G['cache']['plugin']['dzapp_wxpay']['appid'];
			$appsecret = $_G['cache']['plugin']['dzapp_wxpay']['appsecret'];
			if(empty($_GET['code'])) {
				$state = urlencode($_GET['referer']);
				$redirect_uri = urlencode('http://'.$_SERVER['HTTP_HOST'].'/'.$redirect_uri);
				dheader("Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_base&state=$state#wechat_redirect");
			}
			$output = get('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$_GET['code'].'&grant_type=authorization_code');
			$output = json_decode($output, true);
			dsetcookie('openid', $output['openid'], 2592000);
		}
		require DISCUZ_ROOT.'./source/plugin/dzapp_base/base.class.php';
		$jssdk = new mobileplugin_dzapp_base();
		$signPackage = $jssdk->getSignPackage();
		$_G['appId'] = $signPackage['appId'];
		$_G['timestamp'] = $signPackage['timestamp'];
		$_G['nonceStr'] = $signPackage['nonceStr'];
		$_G['signature'] = $signPackage['signature'];
	}
}

function get($url) {

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_URL, $url);

	$res = curl_exec($curl);
	curl_close($curl);

	return $res;
}

function post($url, $data) {

	$curl = curl_init();
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

	$res = curl_exec($curl);
	curl_close($curl);

	return $res;
}

function basecmp($a, $b) {
	return $a['displayorder'] > $b['displayorder'] ? 1 : -1;
}

?>